using System.Configuration;

namespace KexpScrobbler
{
    public delegate void ConfigurationChangedEventHandler(object sender, ConfigurationChangedEventArgs e);

    public class ConfigurationSingleton
    {
        static Configuration config;
        static KexpScrobblerConfigurationSection section;

        private ConfigurationSingleton()
        {
        }

        public static event ConfigurationChangedEventHandler ConfigurationChanged;

        private static KexpScrobblerConfigurationSection Section
        {
            get
            {
                if (config == null)
                {
                    config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                    section = config.GetSection("KexpScrobbler") as KexpScrobblerConfigurationSection;
                }
                
                return section;
            }
        }

        public static string Username
        {
            get { return Section.Username; }
            set
            {
                Section.Username = value;
                OnConfigurationChanged(KexpScrobblerConfigurationSection.UsernameProperty);
            }
        }

        public static string Password
        {
            get { return Section.Password; }
            set
            {
                Section.Password = value;
                OnConfigurationChanged(KexpScrobblerConfigurationSection.PasswordProperty);
            }
        }

        public static bool Balloon
        {
            get { return Section.Balloon; }
            set
            {
                Section.Balloon = value;
                OnConfigurationChanged(KexpScrobblerConfigurationSection.BalloonProperty);
            }
        }

        protected static void OnConfigurationChanged(ConfigurationProperty configurationProperty)
        {
            if(ConfigurationChanged != null)
            {
                ConfigurationChanged(null, new ConfigurationChangedEventArgs(configurationProperty));
            }
        }

        public static void Save()
        {
            KexpScrobblerConfigurationSection section = Section;
            config.Save();
        }
    }
}